package com.dream21th.generate.util;

import com.dream21th.generate.dto.MysqlDTO;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;

public class DBUtils {
    private Connection con;

    private PreparedStatement pstm;

    private String user = "root";

    private String password = "cw0924Fly";

    private String className = "com.mysql.jdbc.Driver";

    private String url = "jdbc:mysql://localhost:3306/db_dxgw?useUnicode=true&amp;amp;amp;amp;amp;amp;characterEncoding=utf-8";

    public DBUtils() {
        try {
            Class.forName(className);
        } catch (ClassNotFoundException e) {
            e.printStackTrace();
        }
    }


    public Connection getCon() {
        try {
            con = DriverManager.getConnection(url, user, password);
        } catch (SQLException e) {
            con = null;
            e.printStackTrace();
        }
        return con;
    }

    public Connection getCon(MysqlDTO dto) {
        try {
            con = DriverManager.getConnection(dto.getUrl(), dto.getUser(), dto.getPassword());
        } catch (SQLException e) {
            con = null;
            e.printStackTrace();
        }
        return con;
    }

    public void queryTableInfo(MysqlDTO dto){
        getCon(dto);
        String sql="SELECT * FROM information_schema.`COLUMNS` WHERE TABLE_SCHEMA="+"'"+dto.getDataBase()+"'"+"" +
                " and TABLE_NAME="+"'"+dto.getTableName()+"'"+"";

        if (con != null) {
            try {
                System.out.println(sql);
                pstm = con.prepareStatement(sql,
                        ResultSet.TYPE_SCROLL_INSENSITIVE,
                        ResultSet.CONCUR_READ_ONLY);
                pstm.execute();
            } catch (SQLException e) {

                e.printStackTrace();
            }
        }
    }

    public void queryTableInfo(MysqlDTO dto,String sql){
        getCon(dto);
        if (con != null) {
            try {
                System.out.println(sql);
                pstm = con.prepareStatement(sql,
                        ResultSet.TYPE_SCROLL_INSENSITIVE,
                        ResultSet.CONCUR_READ_ONLY);
                pstm.execute();
            } catch (SQLException e) {

                e.printStackTrace();
            }
        }
    }

    public void doPstm(String sql, Object[] params) {
        if (sql != null && !sql.equals("")) {
            if (params == null)
                params = new Object[0];

            getCon();
            if (con != null) {
                try {
                    System.out.println(sql);
                    pstm = con.prepareStatement(sql,
                            ResultSet.TYPE_SCROLL_INSENSITIVE,
                            ResultSet.CONCUR_READ_ONLY);
                    for (int i = 0; i < params.length; i++) {
                        pstm.setObject(i + 1, params[i]);
                    }
                    pstm.execute();
                } catch (SQLException e) {

                    e.printStackTrace();
                }
            }
        }
    }

    public ResultSet getRs() throws SQLException {
        return pstm.getResultSet();
    }

    public int getCount() throws SQLException {
        return pstm.getUpdateCount();
    }

    public void closed() {
        try {
            if (pstm != null)
                pstm.close();
        } catch (SQLException e) {

            e.printStackTrace();
        }
        try {
            if (con != null) {
                con.close();
            }
        } catch (SQLException e) {
            e.printStackTrace();
        }
    }

   /* public static void main(String[] args) throws SQLException {
        String sql = "select * from t_user where loginname=? and loginpw=? and del='no'";
        Object[] params = {"", ""};
        DBUtils mydb = new DBUtils();
        mydb.doPstm(sql, params);
        ResultSet rs = mydb.getRs();
    }*/
}
